package multidraw;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import multidraw.model.DrawingCanvasModel;
import multidraw.model.ModelObserver;



public class ControlPanelController implements ActionListener, ItemListener, ModelObserver {
	private DrawingCanvasModel myModel;
	private ControlPanelView myView;
	public ControlPanelController(DrawingCanvasModel model, ControlPanelView view) {
		this.myModel = model;
		this.myView = view;
		
	}

	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if (e.getStateChange() == ItemEvent.SELECTED) {
			if (myModel.getSelectedObject() != null) {
				myModel.getSelectedObject().setPenColor(itemToColor(e.getItem()));
				myModel.notifyObservers();
			} else {
		      myModel.setPenColor(itemToColor(e.getItem()));
			}			
		}
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub		
		if (e.getSource().equals(myView.getFillButton()))
			myModel.switchSelectedObjectFilling();
		else
			myModel.clearCanvas();
	}

	protected Color itemToColor(Object item) {
	    if( item == null ) {
	      return myModel.getPenColor();
	    }
	    
	    if("black".equals(item)) {
	      return Color.black;
	    }
	    else if("blue".equals(item)) {
	      return Color.blue;
	    }
	    else if("green".equals(item)) {
	      return Color.green;
	    }
	    else if("red".equals(item)) {
	      return Color.red;
	    }
	    else {
	      return myModel.getPenColor();
	    }
	  }
	
	public void updateContent() {
		
	}
}
